package com.asman.vr.utils

import java.net.URLDecoder

/**
 *    author : ybk
 *    date   : 2019-07-14 17:58
 *    desc   :
 */
object UrlUtils {

    /**
     *  转换Url成 Map<String,String>
     *  @param url 例如 asman://com.asman.xiaoniuge/aaa/aaa?key1=value1&key2=value2
     *  return
     */
    fun formatUrl2Map(urlStr: String?): Map<String, String>? {
        val url = URLDecoder.decode(urlStr, "UTF-8")
        if (url.isNullOrEmpty()) return null
        return try {
            if (url.contains("?")) {
                val paramsStr = url.substringAfter("?")
                val params = paramsStr.split("&") //["key1=value1","key2=value2"]
                val map = hashMapOf<String, String>()
                params.map {
                    if (it.isNotEmpty()) {
                        val kv = it.split("=") //["key1","value1"]
                        if (kv.size == 2) {
                            map[kv[0]] = kv[1]
                        }
                    }
                }
                map
            } else {
                null
            }
        } catch (e: Exception) {
            null
        }
    }


}